
<?php 
	if(isset($_GET["month"])) $month = $_GET["month"];
	else $month = date("n");
	if(isset($_GET["year"])) $year = $_GET["year"];
	else $year = date("Y");
	
	$numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
	$firstDay = date("w", mktime(12, 0, 0, $month, 1, $year));
	
	require_once 'function.php';
	
	$monthName = getMonthName($month);
	
	$next = $month + 1;
	if($next>12)$next -= 12;
	$prev = $month - 1;
	if($prev<1)$prev += 12;
	
	$start = true;
	$end = false;
	$day = 1;
	?>
<table class="calendar">
<tr>
	<td colspan="7" class="top"><?php echo $year;?></td>
</tr>
<tr>
	<td class="top"><span class="link" onclick="getCalendar(<?php echo $prev;?>, <?php if($prev==12)echo $year-1; else echo $year;?>);"><<</span></td>
	<td colspan="5" class="top"><?php echo $monthName;?></td>
	<td class="top"><span class="link" onclick="getCalendar(<?php echo $next;?>, <?php if($next==1)echo $year+1; else echo $year;?>);">>></span></td>
<!-- 	<td class="top"><a href="?s=calendar&month=<?php echo $prev;?>&year=<?php if($prev==12)echo $year-1; else echo $year;?>" class="calendar"><<</a></td>
	<td colspan="5" class="top"><?php echo $monthName;?></td>
	<td class="top"><a href="?s=calendar&month=<?php echo $next;?>&year=<?php if($next==1)echo $year+1; else echo $year;?>" class="calendar">>></a></td>
 -->
</tr>
<tr class="header">
	<td>pon</td>
	<td>wt</td>
	<td>śr</td>
	<td>czw</td>
	<td>pia</td>
	<td>sob</td>
	<td onmouseover="" onmouseout="">nd</td>
</tr>
<?php 	

	if(!isset($base)){
		include 'dbmanager.php';
		$base = new BazaDanych();
	}

	while(!$end){
		echo "<tr>";
		for ($i = 1; $i <= 7; $i++){
			$class = "calendar";
			if($i >= 6) $class = "weekend";
			
			$wyniki = $base->pobierzTabliceWynikow("select * from events where Date='".$year."-".$month."-".$day."';");
			$counter = 0;
			foreach ($wyniki as $x) $counter++;
			if($counter > 0) {
				$class .= "Full";
			}
			
			if($i < $firstDay && $start) echo "<td class=\"".$class."N\">&nbsp;</td>";
			else {
				$start = false;
				if(!$end) echo "<td class=\"".$class."\" onmouseover=\"hoverOn(this);\" onmouseout=\"hoverOff(this);\" onclick=\"eventList(".$day.",".$month.",".$year.");\">" . $day . "</td>";
				else echo "<td class=\"".$class."N\">&nbsp;</td>";
				$day++;
				if($day>$numDays) {
					$day = 1;
					$end = true;
				}
			}
		}
		echo "</tr>";
	}
?>
</table>